<template>
  <div>
    <el-table :data="fractionTable" style="width: 100%;">
      <el-table-column label="id" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.id }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="姓名" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.name }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="班级" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.class }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="成绩" width="150">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.fraction }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="upFractionTable(scope.$index,)">修改</el-button>
         
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :data="upFraction" title="设置成绩信息" :visible.sync="isShowUp" width="30%">
      <el-form>
        <!-- <template slot-scope="scope"> -->
        <el-form-item label="编号">
          <el-input :disabled="true" v-model="upFraction.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input :disabled="true" v-model="upFraction.name"></el-input>
        </el-form-item>
        <el-form-item label="班级">
          <el-input :disabled="true" v-model="upFraction.class"></el-input>
        </el-form-item>
        <el-form-item label="成绩">
          <el-input v-model="upFraction.fraction"></el-input>
        </el-form-item>
        <!-- </template> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updFraction(upFraction)">确 定</el-button>
        <el-button @click="isShowUp = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
export default {
  // props:["student"],
  data() {
    return {
      // 学生表总信息
      fractionTable: [],
      // 点击添加时出现弹窗
      isShowSet: false,
      // 点击修改时出现弹窗
      isShowUp: false,
      // 添加的学生信息
      student: {},
      // 修改时获取的当前索引的学生信息
      upFraction: {},
    };
  },
  methods: {
    // ------------------------------成绩页面显示内容-----------------------------
    // 查询成绩列表，显示在页面上
    getFraction() {
      axios
        .get(`${process.env.VUE_APP_BASE_URL}/fraction`)
        .then((res) => {
          this.fractionTable = res.data;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 设置成绩信息
    updFraction() {
      axios
        .put(`${process.env.VUE_APP_BASE_URL}/fraction`, { student: this.upFraction })
        .then((res) => {
          this.$message({
            type: "success",
            message: "修改成功!",
          });
          this.getFraction();
          this.isShowUp = false;
          console.log(res);
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 用于获取需要修改的成绩信息，显示在弹窗内
    upFractionTable(index) {
      this.isShowUp = true;
      for (let i = 0; i <= this.fractionTable.length; i++) {
        if (i == index) {
          this.upFraction = this.clone(this.fractionTable[i]);
          console.log(this.upFraction)
        }
      }
    },
    // 克隆数据
    clone(obj) { // {name:"123"}
      let newObj = {}
      for(var key in obj) {
        newObj[key] = obj[key]
      }
      return newObj
    }
  },
  created() {
    this.getFraction();
  },
};
</script>
<style scoped>
.inst {
  background-color: rgb(94, 189, 245);
  float: right;
  margin: 20px 50px 0px 0px;
  color: white;
}
</style>